<template>
  <div class="songcate">
    <div class="catecur" @click="showCard = !showCard">
      <div>{{currentCate}}</div>
      <span class="iconfont iconarrow-right"></span>
      <cate-card v-if="showCard">
        <div class="allSong" @click="currentCate = '全部歌单'">
          <span :class="{currentCate:currentCate === '全部歌单'}">全部歌单</span>
        </div>
        <div v-for="(cate,index) in allCate" :key="index">
          <div class="cateList">
            <div class="cateHeader">{{cate.name}}</div>
            <div class="cateInfo">
              <div v-for="(cateinfo,index) in cate.cate" :key="index">
                <span :class="{currentCate:cateinfo === currentCate}" @click="changeCate(cateinfo)">{{cateinfo}}</span>
              </div>
            </div>
          </div>
        </div>
      </cate-card>
    </div>
    <div class="categood">
      <span
        v-for="item in hotCate"
        :key="item.id"
        :class="{currentCate:item.name === currentCate}"
        @click="changeCate(item.name)"
      >{{item.name}}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
type allCateT = string[]
import { onMounted, ref, defineEmits } from 'vue'
import axios from '@/http.ts'
import cateCard from '@/components/catecard'
const currentCate = ref<string>('全部歌单')
const hotCate = ref<Array<string>>([])
const allCate = ref<Array<allCateT>>([])
onMounted(() => {
  gethotCate()
  getAllCate()
})
const emit = defineEmits(['changeCate'])
const showCard = ref<boolean>(false)
const changeCate = cateinfo => {
  currentCate.value = cateinfo
  emit('changeCate', cateinfo)
}
const getAllCate = () => {
  axios.get('/playlist/catlist').then(res => {
    const cateDetailList = []
    const { categories, sub } = res.data
    Object.values(categories).map(item => {
      cateDetailList.push({ name: item, cate: [] })
    })
    sub.forEach(items => {
      cateDetailList.forEach(item => {
        if (item.name === categories[items.category]) {
          item.cate.push(items.name)
        }
      })
    })
    allCate.value = cateDetailList
  })
}
const gethotCate = () => {
  axios.get('playlist/hot').then(res => {
    hotCate.value = res.data.tags
  })
}
</script>

<style lang="less" scoped>
.songcate {
  display: flex;
  margin: 20px 0;
  justify-content: space-between;
  .catecur {
    position: relative;
    border: 1px solid #454545;
    width: 100px;
    height: 30px;
    border-radius: 30px;
    color: #b4b4b4;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #252525;
    font-size: 13px;
    font-weight: bold;
    &:hover {
      background-color: #303030;
      cursor: pointer;
    }
    .allSong {
      font-size: 14px;
      padding: 20px 100px;
      border-bottom: 1px solid #4a4a4a;
      &:hover {
        color: #c3473a;
        cursor: pointer;
      }
    }
    .cateList {
      display: flex;
      justify-content: space-between;
      margin: 20px 0;
      .cateHeader {
        flex-basis: 200px;
        text-align: center;
        margin-top: 5px;
        color: #565656;
        font-weight: normal;
      }
      .cateInfo {
        width: 80%;
        display: flex;
        flex-wrap: wrap;
        div {
          flex-basis: 120px;
          text-align: left;
          margin: 5px 0;
          span {
            &:hover {
              color: #c3473a;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
  .categood {
    color: #878787;
    font-size: 12px;
    line-height: 30px;
    span {
      margin-left: 40px;
      &:hover {
        color: #b2b2b2;
        cursor: pointer;
      }
    }
  }
}
.currentCate {
  color: #a93521 !important;
  background-color: #3d3736 !important;
  padding: 4px 12px;
  border-radius: 20px;
}
</style>